{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义数据链表类\n",
    "class ListNode:\n",
    "    def __init__(self, val=None, Next=None):\n",
    "        self.data = val\n",
    "        self.next = Next\n",
    "class LinkedList:\n",
    "    def __init__(self):                                  \n",
    "        self.dummy_head = ListNode()                     \n",
    "        self.size = 0                                     \n",
    "        \n",
    "    def create(self, v_list):                    # 从数组中创建链表\n",
    "        for val in v_list:\n",
    "            last_node = self.dummy_head\n",
    "            while last_node.next:\n",
    "                last_node = last_node.next                    \n",
    "            last_node.next = ListNode(val)                   \n",
    "            self.size +=1                                    \n",
    "        \n",
    "    def print(self):\n",
    "        cur_node = self.dummy_head\n",
    "        while cur_node:\n",
    "            print(cur_node.data, end=\" -> \")\n",
    "            cur_node = cur_node.next\n",
    "        print(\"None\")                                  \n",
    "        print(self.size)                             \n",
    "        \n",
    "    #双指针法\n",
    "    def algorithm1(self):\n",
    "        pre = self.dummy_head.next              # 链表第一个节点\n",
    "        cur = self.dummy_head.next.next         # 链表第二个节点\n",
    "        pre.next = None                         # 将第一个节点的下一链接设为None，代表当前第一个节点将作为新链表的尾部\n",
    "        while cur:                              # 循环结束时，pre指向尾节点，cur为尾节点的None指向\n",
    "            tmp = cur.next                      ## 暂存cur的下一节点，用于循环向后更新\n",
    "            cur.next = pre                      ## 将cur指向翻转\n",
    "            pre = cur                           ## 更新向后移动pre\n",
    "            cur = tmp                           ## 更新向后移动cur\n",
    "        self.dummy_head = ListNode()\n",
    "        self.dummy_head.next = pre              ## 重新设置虚拟头节点，将当前尾部pre和虚拟头节点链接\n",
    "    \n",
    "    #递归法\n",
    "    def algorithm2(self):\n",
    "        pre = self.dummy_head.next              # 链表第一个节点\n",
    "        cur = self.dummy_head.next.next         # 链表第二个节点\n",
    "        pre.next = None                         # 将第一个节点的下一链接设为None，代表当前第一个节点将作为新链表的尾部\n",
    "        return self.reverse(cur, pre)\n",
    "    def reverse(self, cur, pre):                # 递归主循环函数：不断运行自身构成“复合函数”\n",
    "        if cur == None:\n",
    "            self.dummy_head = ListNode()\n",
    "            self.dummy_head.next = pre\n",
    "            return                              # 递归结束条件：同双指针将原链表最后一个节点变为头节点操作\n",
    "        tmp = cur.next\n",
    "        cur.next = pre                          # 同双指针变换前后指向操作\n",
    "        return self.reverse(tmp, cur)           # 重新调用自身函数，注意：参数设置：cur->tmp（cur.next暂存版）;pre->cur，相当于更新后移\n",
    "    \n",
    "    # 三指针法：pre,cur,next\n",
    "    def algorithm3(self):\n",
    "        pre = self.dummy_head\n",
    "        cur = pre.next\n",
    "        while cur.next:\n",
    "            tmp = cur.next                      # 第三个指针next，对应cur.next\n",
    "            cur.next = tmp.next                 # 将头节点cur链接后续节点\n",
    "            tmp.next = pre.next                 # 链表节点反转，反转cur和next链接\n",
    "            pre.next = tmp                      # 将尾节点和前面节点链接pre"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#实例化\n",
    "linked_list = LinkedList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = list(range(10))\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> None\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "linked_list.create(nums)\n",
    "linked_list.print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#翻转链表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 -> None\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "#双指针法\n",
    "linked_list.algorithm1()\n",
    "linked_list.print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> None\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "#递归（在双指针结果上操作翻转：变回原样）\n",
    "linked_list.algorithm2()\n",
    "linked_list.print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 -> None\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "#三指针法\n",
    "linked_list.algorithm3()\n",
    "linked_list.print()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
